草庐IT

C++ unique_ptr 和映射

全部标签

c++ - shared_ptr 如何破坏对齐

我正在阅读有关DirectXMath的文档,无意中发现了下一段:AsanalternativetoenforcingalignmentinyourC++classdirectlybyoverloadingnew/delete,youcanusethepImplidiom.IfyouensureyourImplclassisalignedvia__aligned_mallocinternally,youcanthenfreelyusealignedtypeswithintheinternalimplementation.Thisisagoodoptionwhenthe'public'cl

c++ - 比较 shared_ptr 对象是否相等

是否有标准谓词来比较shared_ptr托管对象的相等性。templateinlinebooltarget_equal(constT&lhs,constU&rhs){if(lhs&&rhs){return*lhs==*rhs;}else{return!lhs&&!rhs;}}我想要类似于上面代码的东西,但如果已经有标准解决方案,我会避免自己定义它。 最佳答案 不,没有标准的解决方案。shared_ptr等的相等运算符只比较指针而不比较托管对象。你的解决方案很好。我建议这个版本检查指向的对象是否相同,如果共享指针之一为null而另一个

c++ - std::shared_ptr operator [] 等效访问

在C++17中,std::shared_ptr有一个运算符[]以允许索引基于vector的指针(http://en.cppreference.com/w/cpp/memory/shared_ptr/operator_at)如果这样的运算符不可用,我如何获得类似的访问,我仍然想对元素数组使用智能指针,例如:std::shared_ptrdata;data.reset(newunsignedchar[10]>;//usedata[3]; 最佳答案 像这样:data.get()[3]但是,请记住Nathan在评论中所说的话。std::sh

c++ - `weak_ptr` 和 `shared_ptr` 访问如何是原子的

std::shared_ptrint_ptr;intmain(){int_ptr=std::make_shared(1);std::threadth{[&](){std::weak_ptrint_ptr_weak=int_ptr;autoint_ptr_local=int_ptr_weak.lock();if(int_ptr_local){cout上面的代码线程安全吗?我读了这个答案Aboutthread-safetyofweak_ptr但只是想确保上面的代码是线程安全的。我问这个的原因是,如果上面的代码确实是线程安全的,我无法理解std::weak_ptr是如何实现的。和std::s

c++ - 在 C++ 中初始化映射和删除的正确方法

我正在尝试创建在我的类的构造函数中声明的静态映射。该映射将在一种方法中初始化并填充数据,并在另一种方法中释放。这是正确的做法吗?usingnamespacestd;#includestructa{stringb;stringc;}classaClass:publicmyClass{public:aClass();virtual~aClass();private:mapmyMap;voidmethod(inta);voidamethod(intb);}voidaClass::method(inta){myMap=newmap;//Additionofelements;}voidaClas

c++ - 是否可以在 Xcode 5.1 中使用 std::make_unique?

由于Xcode5.1包含clang3.4,因此应该可以使用std::make_unique。好像是在memory.h中定义的。但是,它需要有_LIBCPP_STD_VER>11但由于__cplusplus宏的值,它仍然设置为11(仍然是201103L).有办法改变吗? 最佳答案 如clangwebsite中所述,您需要启用-std=c++1y。Xcode在其“C++语言版本”选项中不包含此选项作为选项,因此您需要手动输入它。为此,您需要在项目定义打开时进入“编辑器”菜单,然后按“显示定义”。您现在应该能够手动将“C++语言方言”选项

c++ - 通过采用 void * 的 C 接口(interface)传递 shared_ptr

我有一个使用SDL的C++项目,特别是SDL事件。我想将事件系统用于传入的网络消息,就像它用于UI事件一样。我可以定义一个新的事件类型并附加一些任意数据(参见thisexample)。如果我使用普通指针,这就是我会做的:Uint32message_event_type=SDL_RegisterEvents(1);/*Inthemaineventloop*/while(SDL_Poll(&evt)){if(evt.type==message_event_type){Message*msg=evt.user.data1;handle_message(msg);}}/*Networkingc

c++ - std::move 和映射赋值

我对标准如何管理这种情况感到有点困惑:structFoo{Foo&operator=(std::stringxxx){x=std::move(xxx);return*this;}std::stringx;};std::mapbar;std::stringbaz="somestring";bar[baz]=std::move(baz);编译器能否生成代码,以便baz被movebefore它用于初始化和获取对bar中元素的引用(初始化std::stringxxx)?或者这段代码是否安全并且没有未定义的行为? 最佳答案 hell不。表达式

c++ - "Use of undefined type"带有 unique_ptr 以转发声明的类和默认移动构造函数/赋值

在下面的代码中,是避免编译错误并在A.cpp中手动包含B.h实现移动构造函数/赋值的唯一方法吗?//A.h#includeclassB;//implementationsomewhereinB.h/B.cppclassA{public:A()=default;~A()=default;A(constA&)=delete;A&operator=(constA&)=delete;A(A&&)=default;A&operator=(A&&)=default;private:std::unique_ptrm_b;};VisualStudio2015给出“错误C2027:使用未定义类型”,因为

c++ - 如何使用 std::greater 对 C++ 映射键进行排序?

我正在创建一个std::map在C++中,我更愿意让它们的键从最高到最低排序,而不是默认的排序顺序。我的研究让我找到了std::greater看起来很有前途,但在尝试使用它时出现编译错误:invalidtypeargumentofunary‘*’(have‘int’)我的map声明是:std::map>numMap;这个函数会抛出错误:voidRow::addNumber(intnum,intpos){numMap.insert(num,pos);}类似问题的答案,例如this在声明中包含括号,即std::greater()-但是当我包含这些括号时,我会收到关于函数返回函数的多个错误。